﻿using System.Collections.Generic;
using AgileBoard.Domain;

namespace AgileBoard.Dtos.Mappers
{
    public class IterationDTOMapper : BaseDTOMapper
    {
        public static IterationDTO MapToDTO(Iteration model)
        {
            double sizeSum = 0.0;
            double evaluation = 0.0;
            double remaining = 0.0;
            var dto = new IterationDTO();
            IList<StoryDTO> storyDTO = new List<StoryDTO>();

            dto.ID = model.IterationID;
            dto.ReleaseID = model.ReleaseID;
            dto.Name = model.Name;
            dto.PeriodFrom = model.PeriodFrom;
            dto.PeriodTo = model.PeriodTo;
            dto.CreateTime = model.CreateTime;
            dto.Creator = model.Creator;

            //load features
            foreach (Story story in model.Stories)
            {
                sizeSum += story.Size;
                StoryDTO StoryDto = StoryDTOMapper.MapToDTO(story);
                evaluation += StoryDto.EvaluationSum;
                remaining += StoryDto.RemainingSum;
                storyDTO.Add(StoryDto);
            }
            dto.items = storyDTO;

            return dto;
        }

        public static Iteration MapFromDTO(IterationDTO dto)
        {
            var Iteration = new Iteration();
            Iteration.IterationID = dto.ID;
            Iteration.ReleaseID = dto.ReleaseID;
            Iteration.Name = dto.Name;
            Iteration.PeriodFrom = dto.PeriodFrom.ToLocalTime();
            Iteration.PeriodTo = dto.PeriodTo.ToLocalTime();
            Iteration.CreateTime = dto.CreateTime;
            Iteration.Creator = dto.Creator;

            //load features
            foreach (StoryDTO storyDTO in dto.items)
            {
                Iteration.AddStory(StoryDTOMapper.MapFromDTO(storyDTO));
            }

            return Iteration;
        }
    }
}